package classes.Windows;

import db.Jdbc;

import javax.swing.*;
import java.awt.*;
import java.io.File;
import java.sql.ResultSet;
import java.util.Random;

/**
 * @Auther: 程嘉明
 * @Date: 2018/8/26/026 08:49
 * @Description: 启动主界面
 */
public class WinMain extends Window {
    private int targetNum;
    private String[] passes;

    public WinMain(int length, int width, String title) throws Exception {
        super(length, width, title);
        //标题
        JLabel lab1 = new JLabel("                                 智慧工地管理系统");
        lab1.setFont(new Font("宋体", 1, 18));
        lab1.setBounds(0, 30, 800, 60);
        lab1.setOpaque(true);
        lab1.setBackground(Color.blue);
        this.addP(lab1);
        //账号
        JLabel lab2 = new JLabel("账号：");
        JTextField jf1 = new JTextField();
        jf1.setBounds(310, 130, 200, 40);
        lab2.setBounds(230, 120, 80, 60);
        lab2.setFont(new Font("宋体", 1, 18));
        lab1.setOpaque(true);
        this.addP(lab2);
        this.addP(jf1);
        //密码
        JLabel lab3 = new JLabel("密码：");
        JPasswordField jp1 = new JPasswordField();
        lab3.setBounds(230, 200, 80, 60);
        lab3.setFont(new Font("宋体", 1, 18));
        jp1.setBounds(310, 210, 200, 40);
        this.addP(lab3);
        this.addP(jp1);
        //登录
        JButton jb1 = new JButton("登录");
        jb1.setBounds(340, 380, 120, 50);
        this.addP(jb1);
        //验证码
        JLabel lab4 = new JLabel("输入验证码：");
        lab4.setBounds(180, 270, 180, 60);
        lab4.setFont(new Font("宋体", 1, 18));
        this.addP(lab4);
        /**
         * 首先创建一个画板获取所有验证码的图片名称，图片名称和验证码密码对应相同
         * 创建图片和按钮
         */
        class PassComponent extends JPanel {
            private Image image;//图片
            //画入验证码的位置长宽
            private int length;
            private int width;
            private int x;
            private int y;

            public PassComponent(int x, int y, int length, int width) {
                this.changeNum();//初始化一张验证码
                this.setLayout(null);//绝对布局
                this.length = length;
                this.width = width;
                this.x = x;
                this.y = y;
                this.setVisible(true);
                this.setOpaque(true);
            }

            @Override
            public void paintComponent(Graphics g) {
                if (image == null) {
                    System.out.println("image为空");
                }
                g.drawImage(image, x, y, length, width, null);//绘画
            }

            /**
             * 更改一张验证码
             */
            public void changeNum() {
                File file = new File("./src/picture/verification_code");//存储验证码图片的文件夹
                //保证文件夹中只有图片不在出现文件夹，遍历时不考虑文件夹
                File[] files = file.listFiles();//文件数组
                passes = new String[10];//存储验证码答案有序
                for (int i = 0; i < files.length; i++) {
                    passes[i] = files[i].getName().split(".png")[0];
                }
                //加入图片
                Random rand = new Random();//随机加入
                targetNum = rand.nextInt(10);
                String pass = passes[targetNum];
                String path = "./src/picture/verification_code/" + pass + ".png";
                this.image = new ImageIcon(path).getImage();
                this.repaint();//重画后就会默认调用paintComponent()方法
            }
        }
        PassComponent pass = new PassComponent(0, 0, 120, 40);//图片在画板的画出位置
        pass.setBounds(312, 275, 120, 40);//画板在背景图片的位置
        pass.setVisible(true);
        this.addP(pass);
        JTextField jf2 = new JTextField();
        jf2.setBounds(450, 275, 100, 40);
        jf2.setVisible(true);
        this.addP(jf2);
        JButton jb2 = new JButton("看不清，换一张");
        jb2.setBounds(550, 275, 120, 40);
        jb2.setVisible(true);
        jb2.addActionListener(e -> {
            pass.changeNum();
        });
        this.addP(jb2);
        JButton jb3 = new JButton("退出系统");
        jb3.setBounds(80, 460, 120, 50);
        jb3.setVisible(true);
        jb3.addActionListener(e -> {
            int n = JOptionPane.showConfirmDialog(null, "确认退出系统?", "真香警告！", JOptionPane.YES_NO_OPTION);
            if (n == 0) {
                System.exit(0);
            }
        });
        this.addP(jb3);
        JButton jb4 = new JButton("注册");
        jb4.setBounds(650, 460, 120, 50);
        jb4.setVisible(true);
        jb4.addActionListener(e -> {
            try {
                new Register(800, 600, "注册");
            } catch (Exception e1) {
                e1.printStackTrace();
            }
        });
        this.addP(jb4);
        /**
         * 添加登录的按钮事件
         * 先判断验证码
         */
        jb1.addActionListener(e -> {
            //判断验证码是否正确
            String code = jf2.getText();//得到验证码框中信息
            if (!code.equals(passes[targetNum])) {//错误
                JOptionPane.showMessageDialog(null, "验证码错误！", "警告", JOptionPane.ERROR_MESSAGE);
            } else {//正确
                String ID = jf1.getText();//获取ID
                try {
                    Jdbc.getConnection();
                    ResultSet rs = Jdbc.stat.executeQuery("select count(ID) from t_gdryjbxx where ID='" + ID + "';");//判断是否存在这个ID
                    rs.next();
                    if (rs.getInt("count(ID)") == 0) {//不存在ID
                        JOptionPane.showMessageDialog(null, "ID不存在！", "警告", JOptionPane.ERROR_MESSAGE);
                    } else {//存在ID
                        char[] PASS = jp1.getPassword();
                        String PASS1 = String.valueOf(PASS);
                        ResultSet rs1 = Jdbc.stat.executeQuery("select PASS from t_gdryjbxx where ID='" + ID + "';");//判断是否密码和ID匹配
                        rs1.next();
                        if (PASS1.equals(rs1.getString("PASS"))) {
                            JOptionPane.showMessageDialog(null, "登录成功！", "提示", JOptionPane.INFORMATION_MESSAGE);
                            Jdbc.getConnection();
                            ResultSet set = Jdbc.stat.executeQuery("select NAME from  t_gdryjbxx where ID='" + ID + "'");
                            set.next();
                            String NAME = set.getString("NAME");
                            if (ID.charAt(0) == 'T') {
                                new WinWorker(800, 600, "工人操作界面", ID, NAME);
                                this.setVisible(false);
                            } else {
                                new WinManager(800, 600, "经理操作界面", ID, NAME);
                                this.setVisible(false);
                            }
                        } else {
                            JOptionPane.showMessageDialog(null, "密码错误！", "警告", JOptionPane.ERROR_MESSAGE);
                        }
                    }
                } catch (Exception e1) {
                    e1.printStackTrace();
                }
            }
        });
    }

    public static void main(String[] args) throws Exception {
        new WinMain(800, 600, "智慧工地管理系统");
    }

}
